﻿package sch.jl.caigou.db;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import javax.sql.DataSource;

import com.mchange.v2.c3p0.ComboPooledDataSource;

import sch.jl.caigou.exception.DBException;

public class JDBCUtils {

	private static DataSource dataSource = null;
	static
	{
		dataSource = new ComboPooledDataSource("javawebapp");
	}
	
	public static Connection getConnection()
	{
		try
		{
			return dataSource.getConnection();
		}
		catch(SQLException e)
		{
			e.printStackTrace();
			throw new DBException("数据库连接错误");
		}
	}
	public static void release(Connection connection)
	{
		try
		{
			if(connection!=null)
			{
				connection.close();
			}
		}
		catch(SQLException e)
		{
			e.printStackTrace();
			throw new DBException("数据库关闭错误");
		}	
	}
	public static void release(ResultSet rs,Statement statement)
	{
		try
		{
			if(rs!=null)
			{
				rs.close();
			}
		}
		catch(SQLException e)
		{
			e.printStackTrace();
			throw new DBException("数据库关闭连接错误");
		}
		try
		{
			if(statement!=null)
			{
				statement.close();
			}
		}
		catch(SQLException e)
		{
			e.printStackTrace();
			throw new DBException("数据库关闭连接错误");
		}
	}
}
